using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using MobileClient.UIProcessService;

namespace MobileClient
{
	/// <summary>
	/// Summary description for ctlSingleSelectOtherEntry.
	/// </summary>
	public class ctlSingleSelectOtherEntry : Control
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TabControl tabControl;
		private System.Windows.Forms.TabPage tabSelect;
		private System.Windows.Forms.TabPage tabOther;
		private System.Windows.Forms.ComboBox cbSelection;
		private System.Windows.Forms.Label lblOther;
		private System.Windows.Forms.TextBox txtOther;

		private SingleSelectOtherEntry m_objEntry;
		private int m_nSelected = 0;

		public ctlSingleSelectOtherEntry()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			m_objEntry = null;
		}

		public SingleSelectOtherEntry Entry
		{
			get { return m_objEntry; }
			set { m_objEntry = value; }
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tabControl = new System.Windows.Forms.TabControl();
			this.tabSelect = new System.Windows.Forms.TabPage();
			this.tabOther = new System.Windows.Forms.TabPage();
			this.cbSelection = new System.Windows.Forms.ComboBox();
			this.lblOther = new System.Windows.Forms.Label();
			this.txtOther = new System.Windows.Forms.TextBox();
			// 
			// tabControl
			// 
			this.tabControl.Controls.Add(this.tabSelect);
			this.tabControl.Controls.Add(this.tabOther);
			this.tabControl.Location = new System.Drawing.Point(3, 6);
			this.tabControl.SelectedIndex = 0;
			this.tabControl.Size = new System.Drawing.Size(230, 140);
			this.tabControl.SelectedIndexChanged +=new EventHandler(tabControl_SelectedIndexChanged);
			// 
			// tabSelect
			// 
			this.tabSelect.Controls.Add(this.cbSelection);
			this.tabSelect.Location = new System.Drawing.Point(4, 22);
			this.tabSelect.Size = new System.Drawing.Size(222, 114);
			this.tabSelect.Text = "Select";
			// 
			// tabOther
			// 
			this.tabOther.Controls.Add(this.lblOther);
			this.tabOther.Controls.Add(this.txtOther);
			this.tabOther.Location = new System.Drawing.Point(4, 22);
			this.tabOther.Size = new System.Drawing.Size(222, 114);
			this.tabOther.Text = "Other";
			// 
			// cbSelection
			// 
			this.cbSelection.Location = new System.Drawing.Point(5, 47);
			this.cbSelection.Size = new System.Drawing.Size(212, 21);
			// 
			// lblOther
			// 
			this.lblOther.Location = new System.Drawing.Point(5, 33);
			this.lblOther.Size = new System.Drawing.Size(44, 20);
			this.lblOther.Text = "Other:";
			// 
			// txtOther
			// 
			this.txtOther.Location = new System.Drawing.Point(5, 61);
			this.txtOther.Size = new System.Drawing.Size(212, 20);
			this.txtOther.Text = "";
			// 
			// ctlSingleSelectOtherEntry
			// 
			this.Controls.Add(this.tabControl);
			this.Size = new System.Drawing.Size(236, 152);

		}
		#endregion

		public void LoadSettings()
		{
			if(m_objEntry != null)
			{
                txtOther.Text = "";
				tabControl.SelectedIndex = 0;

				cbSelection.Items.Clear();

				if(m_objEntry.Options != null)
				{
					foreach(String strItem in m_objEntry.Options)
					{
						cbSelection.Items.Add(strItem);
					}
				}

                if (m_objEntry.OtherSelected)
                {
                    if (!String.IsNullOrEmpty(m_objEntry.OtherValue))
                    {
                        txtOther.Text = m_objEntry.OtherValue;
                    }
                    else
                    {
                        txtOther.Text = "";
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(m_objEntry.Selections[0]))
                    {
                        cbSelection.SelectedItem = m_objEntry.Selections[0];
                    }
                }
			}
		}

		public void SaveSettings()
		{
			if(m_objEntry != null)
			{
                if (!String.IsNullOrEmpty(txtOther.Text))
                {
                    m_objEntry.Selections[0] = cbSelection.Text;
                }
                else
                {
                    m_objEntry.OtherSelected = true;
                    m_objEntry.OtherValue = txtOther.Text;
                }
			}
		}

		private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
		{
			int nIndex = tabControl.SelectedIndex;
			tabControl.TabPages[m_nSelected].Visible = false;			
			tabControl.TabPages[nIndex].Visible = true;
			m_nSelected = nIndex;
		}
	}
}
